﻿#pragma once

/*******************************************************
 * 计算每个体素内的 LETd:
 *  - Event内先累积计算：
 *       numerator   = Σ (LET_track * dE)
 *       denominator = Σ dE
 *    其中 LET_track = dE / stepLength
 *  - 在 EndOfEvent 时，将 num/den 写入 EvtMap
 *  - 单位约定：
 *    - 这里用 MeV 和 mm 进行计算：LET_track ≈ (MeV / mm)
 *    - 结果 LETd 是 (MeV / mm), 数值上等价于 (keV / μm)
 ********************************************************/

#include <G4Types.hh>
#include <unordered_map>
#include <utility>

#include "G4Step.hh"
#include "G4SystemOfUnits.hh"
#include "G4THitsMap.hh"
#include "G4ThreeVector.hh"
#include "G4VPrimitiveScorer.hh"

class PSSDoseAveragedLET : public G4VPrimitiveScorer
{
public:
    PSSDoseAveragedLET(const G4String& name, G4int depth = 0);
    ~PSSDoseAveragedLET() override;

    // 处理每个step
    G4bool ProcessHits(G4Step* step, G4TouchableHistory* hist) override;

    // 事件周期的管理
    void Initialize(G4HCofThisEvent* HCE) override;
    void EndOfEvent(G4HCofThisEvent* HCE) override;
    void clear() override;
    void DrawAll() override {}
    void PrintAll() override;
#if 0
    // 从 x,y,z 获取 linear index
    G4int GetLinearIdx(G4int x, G4int y, G4int z);
#endif

public:
    using AccumPair = std::pair<G4double, G4double>;  // <numerator, denominator>
private:
    std::unordered_map<G4int, AccumPair> _fAccum;  // 按 voxel 累计
    G4int _fHCID{-99};
    G4THitsMap<G4double>* _fEvtMap{};
};
